package com.ciwong.epaper.modules.me.adapter;

import com.ciwong.epaper.application.EpaperConstant;
import com.ciwong.mobilelib.utils.ViewHolder;
import com.ciwong.epaper.util.download.ProgressCircle;
import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.me.ui.StudyRecordActivity;
import com.ciwong.epaper.modules.me.util.MeUtil;
import com.ciwong.epaper.modules.me.util.UploadFileHelper;
import com.ciwong.epaper.modules.epaper.bean.Answer;
import com.ciwong.epaper.modules.epaper.bean.ModuleInfo;

import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.RelativeSizeSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * 我的学习记录adapter
 *
 * @author res-qqlin
 * @version ciwong v.2.0 2015/7/24
 * @since ciwong v.2.0
 */
public class StudyRecordAdapter extends BaseAdapter
{
    private StudyRecordActivity mContext;

    private List<Answer> mListData;

    /**
     * 构造方法
     *
     * @param context
     *            上下文 listView 控件
     * @param listData
     */
    public StudyRecordAdapter(StudyRecordActivity context,
            List<Answer> listData)
    {
        mListData = listData;
        mContext = context;
    }

    @Override
    public int getCount()
    {
        return mListData == null ? 0 : mListData.size();
    }

    @Override
    public Object getItem(int position)
    {
        return mListData == null || mListData.isEmpty() ? null
                : mListData.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        View view;
        if (convertView == null)
        {
            view = View.inflate(mContext, R.layout.item_my_study_record, null);
        }
        else
        {
            view = convertView;
        }

        Answer myWork = (Answer) getItem(position);


        if(myWork.getModuleId() != ModuleInfo.ModuleInfoId.MODULE_ID_VIDEO_EXPLAIN)
        {
            view.setVisibility(View.VISIBLE);
            ImageView ivRemarked = ViewHolder.get(view, R.id.my_work_item_remark);
            TextView tvName = ViewHolder.get(view, R.id.my_work_item_name);
            ProgressCircle progressCircle = ViewHolder.get(view,
                    R.id.progress_circle);
            progressCircle.setIsArrow(false);
            TextView tvScore = ViewHolder.get(view, R.id.my_work_item_score);
            ImageView arrowRight = ViewHolder.get(view, R.id.iv_arrow_right);



            int submitStatus = myWork.getSubmitStatus();
            int moduleId = myWork.getModuleId();

            if (moduleId == ModuleInfo.ModuleInfoId.MODULE_ID_TEST)
            {
                if (myWork.getWorkStatus() == Answer.WorkStatus.WORK_STATUS_CHECK)
                {
                    tvScore.setVisibility(View.VISIBLE);

                    tvScore.setText(MeUtil.formatScore(myWork.getActualScore()) + "分");
                }
                else
                {
                    tvScore.setVisibility(View.GONE);
                }
            }
            else
            {
                if (moduleId == ModuleInfo.ModuleInfoId.MODULE_ID_LISTEN_SPEAK_EXAM
                        && submitStatus == Answer.SubmitStatus.WORK_NO_SCORE_FAILE)
                {
                    tvScore.setVisibility(View.GONE);

                }else  if (moduleId == ModuleInfo.ModuleInfoId.MODULE_ID_VIDEO_EXPLAIN )
                {

                    tvScore.setVisibility(View.VISIBLE);
                    tvScore.setText(MeUtil.formatScore(myWork.getActualScore()) + "%");

                } else if (myWork.getWorkStatus() == EpaperConstant.SCORE_STATUS_UNCOMPLETE) {

                    tvScore.setVisibility(View.VISIBLE);
                    tvScore.setText("评分中");

                }else {

                    tvScore.setVisibility(View.VISIBLE);
                    tvScore.setText(MeUtil.formatScore(myWork.getActualScore()) + "分");
                }
            }

            // 是否是未识别的作业
            boolean isUnKownCard = myWork
                    .getWorkStatus() == Answer.WorkStatus.WORK_STATUS_UNKNOWN_CARD
                    && myWork
                    .getModuleId() == ModuleInfo.ModuleInfoId.MODULE_ID_TEST;

            if (!isUnKownCard
                    && submitStatus == Answer.SubmitStatus.WORK_SUBMIT_SUCCUED)
            {
                progressCircle.setVisibility(View.GONE);
                arrowRight.setVisibility(View.VISIBLE);
                tvName.setTextColor(mContext.getResources()
                        .getColor(R.color.default_text_color));
                tvName.setText(myWork.getResourceName());
            }
            else if (submitStatus == Answer.SubmitStatus.WORK_SUBMITING)
            {
                UploadFileHelper.UploadFileTask task = UploadFileHelper
                        .getInstance().getUploadFileTask(
                                new UploadFileHelper.UploadFileTask(myWork, 0));
                if (task == null)
                {
                    tvName.setTextColor(
                            mContext.getResources().getColor(R.color.red));
                    tvName.setText(myWork.getResourceName());
                    appendErrorMsg(tvName, myWork.getLogInfo());

                    progressCircle.setVisibility(View.GONE);
                    arrowRight.setVisibility(View.VISIBLE);
                    myWork.setSubmitStatus(
                            Answer.SubmitStatus.WORK_FILE_SUBMIT_FAILE);
                }
                else
                {
                    progressCircle.updateDownloadProgress(
                            task.answer.getProgress() / 100f);
                    progressCircle.setVisibility(View.VISIBLE);
                    arrowRight.setVisibility(View.GONE);
                    tvName.setTextColor(mContext.getResources()
                            .getColor(R.color.default_text_color));
                    tvName.setText(myWork.getResourceName());
                }
            }
            else
            {
                tvName.setTextColor(mContext.getResources().getColor(R.color.red));
                tvName.setText(myWork.getResourceName());
                appendErrorMsg(tvName, myWork.getLogInfo());
                progressCircle.setVisibility(View.GONE);
                arrowRight.setVisibility(View.VISIBLE);
            }

            ivRemarked.setVisibility(!TextUtils.isEmpty(myWork.getComment())
                    ? View.VISIBLE : View.INVISIBLE);


        }else {
            view.setVisibility(View.GONE);
        }



//        if (myWork
//                .getModuleId() == ModuleInfo.ModuleInfoId.MODULE_ID_LISTEN_SPEAK_EXAM
//                && submitStatus == Answer.SubmitStatus.WORK_SUBMIT_SUCCUED
//                && TextUtils.isEmpty(myWork.getComment()))
//        {
//            arrowRight.setVisibility(View.INVISIBLE);
//        }

//        view.setBackgroundColor(arrowRight.getVisibility() == View.VISIBLE
//                ? Color.TRANSPARENT : Color.WHITE);

        return view;
    }

    /**
     * 显示错误提示
     *
     * @param tvName
     * @param errorMs
     */
    private void appendErrorMsg(TextView tvName, String errorMs)
    {
        tvName.append("\n");
        SpannableString ss = new SpannableString(errorMs);
        ss.setSpan(new RelativeSizeSpan(0.7f), 0, ss.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvName.append(ss);
    }

}
